<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="shopList" highlight-current-row v-loading="listLoading"style="width: 100%;" :default-sort = "{prop: 'id'}">
			<!--<el-table-column type="selection" width="55">-->
			<!--</el-table-column>-->
			<el-table-column prop="id" label="物品ID" sortable>
			</el-table-column>
			<el-table-column prop="name" label="物品名称" width="250">
			</el-table-column>
			<!--<el-table-column prop="img" label="物品图片" width="200">-->
				<!--<template slot-scope="scope">-->
					<!--<img :src="scope.row.img" width="50">-->
				<!--</template>-->
			<!--</el-table-column>-->
			<el-table-column prop="integral" label="兑换点数" width="200">
			</el-table-column>
			<el-table-column prop="content" label="物品描述" width="300">
			</el-table-column>
			<el-table-column prop="amount" label="剩余数量" width="150">
			</el-table-column>
			<el-table-column prop="exchangelimit" label="最大交换数" width="150">
			</el-table-column>
			<el-table-column label="操作" min-width="200">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="sizes, total, prev, pager, next" :page-sizes="[15, 30, 50]" @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm"  label-width="100px" :rules="editFormRules" ref="editForm">
				<el-form-item label="物品名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
					<!--<el-select v-model="value" placeholder="请选择">-->
						<!--<el-option-->
								<!--v-for="item in options"-->
								<!--:key="item.value"-->
								<!--:label="item.label"-->
								<!--:value="item.value">-->
						<!--</el-option>-->
					<!--</el-select>-->
				</el-form-item>
				<el-form-item label="兑换点数" prop="integral">
					<el-input v-model="editForm.integral" type="number"></el-input>
				</el-form-item>
				<el-form-item label="剩余数量" prop="amount">
					<el-input v-model="editForm.amount" type="number"></el-input>
				</el-form-item>
				<el-form-item label="物品描述" prop="content">
					<el-input v-model="editForm.content"  type="textarea"></el-input>
				</el-form-item>
				<el-form-item label="最大交换数" prop="exchangelimit">
					<el-input v-model="editForm.exchangelimit" type="number"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit(editForm.id)" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="100px"  :rules="addFormRules" ref="addForm">
				<el-form-item label="物品信息" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="图片上传" prop="id">-->
				<!--<el-upload-->
						<!--class="upload-demo"-->
						<!--action="https://jsonplaceholder.typicode.com/posts/"-->
						<!--:on-preview="handlePreview"-->
						<!--:limit=1-->
						<!--:file-list="uploadImg"-->
						<!--:before-upload="beforeAvatarUpload"-->
						<!--list-type="picture">-->
					<!--<el-button size="small" type="primary">点击上传</el-button>-->
				<!--</el-upload>-->
				<!--</el-form-item>-->
				<el-form-item label="兑换点数" prop="integral">
					<el-input v-model="addForm.integral"  type="number"></el-input>
				</el-form-item>
				<el-form-item label="剩余数量" prop="amount">
					<el-input v-model="addForm.amount"type="number"></el-input>
				</el-form-item>
				<el-form-item label="物品描述" prop="content">
					<el-input v-model="addForm.content" a type="textarea"></el-input>
				</el-form-item>
				<el-form-item label="最大交换数" prop="exchangelimit">
					<el-input v-model="addForm.exchangelimit" type="number"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
                uploadImg:[],
                shopList: [],
				total: 0,
				page: 1,
                pageSize:15,
				listLoading: false,

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入物品名称', trigger: 'blur' }
					],
                    integral: [
                        { required: true, message: '请输入兑换点数', trigger: 'blur' }
                    ],
                    amount: [
                        { required: true, message: '请输入剩余数量', trigger: 'blur' }
                    ],
                    content: [
                        { required: false, message: '请输入物品描述', trigger: 'blur' }
                    ],
                    exchangelimit: [
                        { required: true, message: '请输入最大交换数', trigger: 'blur' }
                    ]
				},
				//编辑界面数据
				editForm: {

				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    name: [
                        { required: true, message: '请输入物品名称', trigger: 'blur' }
                    ],
                    integral: [
                        { required: true, message: '请输入兑换点数', trigger: 'blur' }
                    ],
                    amount: [
                        { required: true, message: '请输入剩余数量', trigger: 'blur' }
                    ],
                    content: [
                        { required: false, message: '请输入物品描述', trigger: 'blur' }
                    ],
                    exchangelimit: [
                        { required: true, message: '请输入最大交换数', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				addForm: {
                    name:'',
                    integral:'',
                    amount:'',
                    content:'',
                    exchangelimit:'',
                    state:1
				}

			}
		},
		methods: {
            handleSizeChange(val) {
                this.pageSize = val
                this.getUsers(this.pageSize);
            },
            handlePreview(file) {
                console.log(file);
            },
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!');
                }
                return isLt2M;
            },
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers(this.pageSize);
			},
			//获取用户列表
			getUsers(page) {
				this.listLoading = true;
                this.$axios.get('/integral-commodities?index='+this.page+'&size='+page,{},this.global().token).then((res)=>{
                    if (res.status == 200){
                        let money = res.data.data
						money.forEach((val)=>{
                            val.integral = parseFloat(val.integral)/1000
						})
                        this.shopList = money.filter(function(item){
                            return (item.state == 1);
                        })

                        this.total = res.data.count
                        this.listLoading = false;
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			},
			//删除
			handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    row.state = 0
                    this.$axios.patch('/integral-commodity/'+row.id,row,this.global().token).then((res)=>{
                        if (res.status == 200){
                            this.$message({message: '删除成功', type: 'success'});
                            this.getUsers(this.pageSize);
                            this.listLoading = false
                        }else{
                            this.$message({message: res.data, type: 'error'});
                        }
                    })
                }).catch(() => {

                });
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
                    name:'',
                    integral:'',
                    amount:'',
                    content:'',
                    exchangelimit:'',
					state:1
				};
			},
			//编辑
			editSubmit: function (rowId) {
                this.editForm.integral = parseFloat(this.editForm.integral)*1000
                this.editForm.amount = parseInt(this.editForm.amount)
                this.editForm.exchangelimit = parseInt(this.editForm.exchangelimit)
                this.$axios.patch('/integral-commodity/'+rowId,this.editForm,this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '修改成功', type: 'success'});
                        this.getUsers(this.pageSize);
                        this.editFormVisible = false
                    }else{
                        this.$message({message: res.data, type: 'error'});
                        this.editForm.integral = parseFloat(this.editForm.integral)/1000
                    }
                })
			},
			//新增
			addSubmit: function () {
                this.addForm.integral = parseInt(this.addForm.integral)*1000
                this.addForm.amount = parseInt(this.addForm.amount)
                this.addForm.exchangelimit = parseInt(this.addForm.exchangelimit)
                this.$axios.post('/integral-commodity',this.addForm,this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '添加成功', type: 'success'});
                        this.addFormVisible = false;
                        this.getUsers(this.pageSize);
                    }else{
                        this.$message({message: res.data, type: 'error'});
                        this.addForm.integral = parseInt(this.addForm.integral)/1000
                    }
                })
			},
		},
		mounted() {
			this.getUsers(this.pageSize);
		}
	}

</script>

<style scoped>

</style>